/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.biz.log.impl;

import com.cgmnx.admin.biz.log.OperationLogBiz;
import com.cgmnx.admin.common.bean.Limiter;
import com.cgmnx.admin.common.bean.Template;
import com.cgmnx.admin.common.exception.Preconditions;
import com.cgmnx.admin.dao.condition.LogCondition;
import com.cgmnx.admin.dao.entity.rbac.OperationLogEntity;
import com.cgmnx.admin.model.enums.rbac.OperationType;
import com.cgmnx.admin.service.rbac.OperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: created  Date: 16/12/15 Time: 上午9:29
 * @version: 1.0
 */
@Slf4j
@Service
public class OperationLogBizImpl implements OperationLogBiz {

    @Autowired
    private OperationLogService operationLogService;

    @Override
    public Void save(final OperationLogEntity entry) {

        return new Template<Void>("OperationLogBizImpl.save") {
            @Override
            protected void check() {
                Preconditions.checkNotNull(entry, "操作日志不能为空");
            }

            @Override
            protected Void exec() throws Exception {

                operationLogService.save(entry);

                return null;
            }
        }.execute();
    }

    @Override
    public OperationLogEntity query(final Integer id) {


        return new Template<OperationLogEntity>("OperationLogBizImpl.save") {
            @Override
            protected void check() {
                Preconditions.checkNotNull(id, "操作日志Id不能为空");
            }

            @Override
            protected OperationLogEntity exec() throws Exception {

                return operationLogService.queryById(id);
            }
        }.execute();
    }

    @Override
    public List<OperationLogEntity> query(final Integer operationId, final OperationType type) {


        return new Template<List<OperationLogEntity>>("OperationLogBizImpl.save") {
            @Override
            protected void check() {
                Preconditions.checkNotNull(operationId, "操作日志Id不能为空");
                Preconditions.checkNotNull(type, "操作日志类型不能为空");
            }

            @Override
            protected List<OperationLogEntity> exec() throws Exception {
                OperationLogEntity entry = new OperationLogEntity();

                List<OperationLogEntity> query = operationLogService.query(entry);

                return query;
            }
        }.execute();
    }

    @Override
    public List<OperationLogEntity> queryPage(final LogCondition condition, final Limiter limiter) {


        return new Template<List<OperationLogEntity>>("OperationLogBizImpl.save") {
            @Override
            protected void check() {
                Preconditions.checkNotNull(condition, "操作日志不能为空");
                Preconditions.checkNotNull(limiter, "操作日志不能为空");
            }

            @Override
            protected List<OperationLogEntity> exec() throws Exception {
                return operationLogService.queryPage(condition, limiter);
            }
        }.execute();
    }

    @Override
    public Integer count(final LogCondition condition) {


        return new Template<Integer>("OperationLogBizImpl.save") {
            @Override
            protected void check() {
                Preconditions.checkNotNull(condition, "操作日志查询不能为空");
            }

            @Override
            protected Integer exec() throws Exception {
                return operationLogService.count(condition);
            }
        }.execute();
    }
}